/**
 * Created by wangbin on 2017/11/22.
 */
$(function() {
    var url = {
        listUndo: ctx+"/costAcct/listUndoList",
        genCheckRecord:ctx+"/costAcct/genCheckRecord",
        cancelRecord: ctx+"/costAcct/cancel",
        appendRecord:ctx+"/costAcct/append",
        listRecord:ctx+"/costAcct/getCheckList",
        listItems:ctx+"/costAcct/listItems",
        invalid:ctx+"/costAcct/invalid",
        requestPay:ctx+"/costAcct/requestPay",
        export:ctx+"/costAcct/export"
    };
    var initQuery = function () {
        qryLeft();
        qryCheckList();
    };
    var qryLeft = function () {
        var param = $("#qry-undo-form").serialize();
        $(top.callService("post",url.listUndo,param,function (data) {
            $("#simple-table tbody").html(data);
        },null,"html"));
    };
    var qryCheckList = function () {
        var param = $("#form_checkList").serialize();
        $(top.callService("post",url.listRecord,param,function (data) {
            $("#simple-table-cost tbody").html(data);

            $(".js_btn_qry_check_detail").unbind("click").click(function (evt) {
                qryCheckDetail($(evt.target).attr("val"));
            });
        },null, "html"));
    };
    var qryCheckDetail = function (checkId) {
    	
        var param = {};
        var t = $("#simple-table-cost").find("input[name='ids']:checked").first();
        param.checkId = checkId || t.val() || "";
       
        if (param.checkId.length<1){
            return;
        }
        $(top.callService("post",url.listItems,param,function (data) {
            $("#check-cost-detail tbody").html(data);
        },null, "html"));
    };

    var init = function() {
        XCF.listener();
        $(".js_selectAll").click(function (evt) {
            var t = $(evt.target).attr("checked");
            var items = $(evt.target).parent().parent().parent().parent().
            parent().find("input[type='checkbox']");
            if (t =="checked"){
                $.each(items,function (idx,item) {
                    $(item).attr("checked",t);
                });
            }else {
                $.each(items,function (idx,item) {
                    $(item).removeAttr("checked");
                });
            }
        });
        $(".js_btn_append").click(function () {
            var ids = "";
            var checkId= "";
            var t = $("#simple-table-cost").find("input[name='ids']:checked").first();
            checkId = t.val();
            if (!checkId ||checkId.length<1){
                alert("请选择对账单号");
                return;
            }
            $.each($("#simple-table").find("input[name='ids']:checked"),
                function (idx,item) {
                    ids = ids.concat(",").concat(item.id);
                });
            if (ids.length<1){
                alert("请选择未对账明细");
                return;
            }
            var param = {};
            param.checkId = checkId;
            param.undoList = ids.substring(1);
            $(top.callService("post",url.appendRecord,param,function(data){
                initQuery();
            }));
        });

        $(".js_btn_gen").click(function () {
        	alert(document.getElementById("requirePayTime").value);
            var ids = "";
            var dep = "";
            $.each($("#simple-table").find("input[name='ids']:checked"),function (idx,item) {
                ids = ids.concat(",").concat(item.id);
                if (idx<1){
                    dep = item.alt;
                }
            });
            if (ids.length<1){
                alert("请选择未对账明细");
                return;
            }
            var param = {};
            param.depart = dep;
            param.undoList = ids.substring(1);
            param.total = "0";
            param.requirePayTime=document.getElementById("requirePayTime").value;
            $(top.callService("post",url.genCheckRecord,param,function(data){
                initQuery();
            }));
        });

        $(".js_btn_invalid_items").click(function () {
            var ids = "";
            var checkId= "";
            $.each($("#check-cost-detail").find("input[name='ids']:checked"),
                function (idx,item) {
                    ids = ids.concat(",").concat(item.id);
                    checkId = item.alt;
                });
            if(ids.length<1){
                alert("选择要操作的数据！")
                return;
            }
            var param = {};
            param.checkId = checkId;//$("#checkId").val();
            param.undoList = ids.substring(1);
            $(top.callService("post",url.invalid,param,function(data){
                qryCheckDetail();
            }));
        });

        $(".js_btn_cancel_check").click(function () {
            var ids = "";
            $.each($("#simple-table-cost").find("input[name='ids']:checked"),function (idx,item) {
                ids = ids.concat(",").concat(item.id);
            });
            if(ids.length<1){
                alert("请选择对账单号！")
                return;
            }
            var param = {};
            param.id = ids.substring(1);//$("#checkId").val();
            //param.undoList = ids.substring(1);
            $(top.callService("post",url.cancelRecord,param,function(data){
                qryCheckDetail();
                initQuery();
            }));
        });

        $(".div-left-search").css("display","none");
        /*列表查询*/
        $(".js_btn_left_search").click(function () {
            $(".div-left-search").css("display","block");
        }) ;
        $(".js_left_query").click(function () {
            $(".div-left-search").css("display","none");
            qryLeft();
        });
        $(".js_btn_qry_check").click(function () {
            qryCheckList();
        });
        $(".js_btn_qry_check_detail").click(function () {
            qryCheckDetail();
        });

        initQuery();
    };

    init();

    $(".js_receipt_input").click(function (evt) {
        var checkId= "";
        var t = $("#simple-table-cost").find("input[name='ids']:checked");
        if (!t || t.length>1){
            return;
        }
        t = $(t).first();
        checkId = t.val();
        if (!checkId ||checkId.length<1){
            alert("请选择对账单号");
            return;
        }
        var diag = new top.Dialog();
        diag.Drag = true;
        diag.Title = "发票信息-(对账单号:"+checkId+")";
        diag.URL = ctx+'/costAcct/editReceipt?checkId='+checkId;
        diag.Width = 600;
        diag.Height = 415;
        diag.CancelEvent = function () { //关闭事件
            /*if (diag.innerFrame.contentWindow.document.
                getElementById('zhongxin').style.display == 'none') {
                //nextPage(${page.currentPage});
            }*/
            diag.close();
        };
        diag.show();
    });
    $(".js_btn_pay_request").click(function () {
    	 var ids = "";
         $.each($("#simple-table-cost").find("input[name='ids']:checked"),function (idx,item) {
             ids = ids.concat(",").concat(item.id);
         });
         if(ids.length<1){
             alert("请选择对账单号！")
             return;
         }
         var param = {};
         param.id = ids.substring(1);
         $(top.callService("post",url.requestPay,param,function(data){
        	 alert(data.desc);
             qryCheckDetail();
             initQuery();
         }));
    });    
});